package jp.tkym.labs.gwt.gantt;

import java.util.Date;

/**
 * Calendar Utility class for GWT.
 * @author takayama
 */
public class GwtCalendar{
	private static final long MILLISECONDS_IN_SECOND = 1000l;
	private static final long SECONDS_IN_MINUTE = 60l;
	private static final long MINUTES_IN_HOUR = 60l;
	private static final long HOURS_IN_DAY = 24l;
	private static final int START_YEAR = 1900;
	
	private static final long MILLISECONDS_IN_DAY = MILLISECONDS_IN_SECOND *
		SECONDS_IN_MINUTE *
		MINUTES_IN_HOUR *
		HOURS_IN_DAY;
	
	/**
	 * reuslt Days ( from - sub )
	 * @param sub
	 * @param from
	 * @return
	 */
	public static int subtractDays(Date from, Date sub){
		long subL = sub.getTime();
		long fromL = from.getTime();
		double subDays = (fromL - subL)/MILLISECONDS_IN_DAY;
		return (int) Math.round(subDays);
	}
	
	public static Date now(){
		return new Date();
	}
	
	public static Date dateFromAddDays(int days){
		Date date = now();
		addDays(date, days);
		return date;
	}
	
	public static void addDays(Date date, int days){
		long now = date.getTime(); 
		long add = now + (days * MILLISECONDS_IN_DAY);
		date.setTime(add);
	}
	
	@SuppressWarnings("deprecation")
	public static int month(Date date){
		return date.getMonth() + 1;
	}

	@SuppressWarnings("deprecation")
	public static int dateOnMonth(Date date){
		return date.getDate();
	}

	@SuppressWarnings("deprecation")
	public static int dateOnWeek(Date date){
		return date.getDay();
	}

	@SuppressWarnings("deprecation")
	public static int year(Date date){
		return date.getYear() + START_YEAR;
	}
	
	public static Date clone(Date date){
		return new Date(date.getTime());
	}
	
	public static boolean isHoliday(Date date){
		int dateOnWeek = dateOnWeek(date);
		if(dateOnWeek==0 || dateOnWeek==6) return true;
		return false;
	}
}